package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.MessageConstant;
import com.itheima.domain.AddressBook;
import com.itheima.mapper.AddressMapper;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @Author: zhuan
 * @Desc: 用户地址模块-业务实现
 * @Date: 2022-05-22 16:44:35
 */
@Service
public class AddressServiceImpl implements AddressService {

		@Autowired
		private AddressMapper addressMapper;
		/**
		 * 功能描述: 新增地址
		 * @param addressBook
		 * @return : int
		 */
		@Override
		public int saveAddress(AddressBook addressBook) {
				int row = 0;
				//1.校验参数
				if(addressBook == null){
						return row;
				}
				//2.补全地址数据 ： user_id
				//2.1从当前线程中获取当前登录用户的ID
				Long userId = BaseContext.getUserId();
				if(userId == null){
						throw new RuntimeException(MessageConstant.NOT_LOGIN);
				}
				addressBook.setUserId(userId);
				row = addressMapper.insert(addressBook);
				return row;
		}
		/**
		 * 功能描述:  查询当前登录用户地址列表 默认地址排在最上面，剩余的按照更新时间排序
		 * @return : java.util.List<com.itheima.domain.AddressBook>
		 */
		@Override
		public List<AddressBook> list() {
				//1.获取当前登录用户ID
				Long userId = BaseContext.getUserId();
				//2.封装请求条件、排序条件
				LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
				wrapper.eq(AddressBook::getUserId,userId);
				wrapper.orderByDesc(AddressBook::getIsDefault,AddressBook::getUpdateTime);
				//3.执行查询
				List<AddressBook> addressBookList = addressMapper.selectList(wrapper);
				return addressBookList;
		}
		/**
		 * 功能描述: 修改用户默认地址
		 * @param id
		 * @return : int
		 */
		@Override
		public int defaultAddr(Long id) {
				int row = 0;
				//1.校验参数
				if(id == null){
						return row;
				}
				//2.获取当前登录用户ID
				Long userId = BaseContext.getUserId();
				if(userId == null){
						return row;
				}
				//3.校验选中地址是否为当前用户地址-----如果在第5步，添加了用户ID条件，该步可以省略
				//4.把所有用户地址设置为“is_default=0”
				LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
				updateWrapper.eq(AddressBook::getUserId,userId);
				updateWrapper.set(AddressBook::getIsDefault,0);
				addressMapper.update(null,updateWrapper);
				//5.把当前选择地址，设置为“is_default=1”
				// update address_book set is_default=1 where user_id = #{} and id = #{}
				LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
				wrapper.eq(AddressBook::getUserId,userId);
				wrapper.eq(AddressBook::getId,id);
				wrapper.set(AddressBook::getIsDefault,1);
				row = addressMapper.update(null,wrapper);
				//6.返回修改当前地址为默认地址的影响行数
				return row;
		}
		/**
		 * 功能描述:  获取用户默认地址
		 * @return : com.itheima.domain.AddressBook
		 */
		@Override
		public AddressBook getDefaultAddr() {
				//1.获取当前登录用户ID
				Long userId = BaseContext.getUserId();
				//2.构建查询条件 user_id、is_default
				LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
				wrapper.eq(AddressBook::getUserId,userId);
				wrapper.eq(AddressBook::getIsDefault,1);
				//3.执行查询
				AddressBook addressBook = addressMapper.selectOne(wrapper);
				//4.封装返回结果
				return addressBook;
		}
}
